package cn.hj.org.service.impl;

import cn.hj.basic.exception.BusinessException;
import cn.hj.basic.service.impl.BaseServiceImpl;
import cn.hj.org.domain.Employee;
import cn.hj.org.domain.Shop;
import cn.hj.org.mapper.EmployeeMapper;
import cn.hj.org.mapper.ShopMapper;
import cn.hj.org.service.IShopService;
import org.assertj.core.internal.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 店铺入驻页面
     * @param shop
     */
    @Override
    public void settleMent(Shop shop) {
        Employee admin = shop.getAdmin();
        //判断2次输入的密码是否相同(不相同抛出异常）
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("2次密码不一致，请重新输入");
        }
        //查询用户名是否被注册
        Employee employee = employeeMapper.loadByUserName(admin.getUsername());
        //如何用户名存在，提示用户直接登录或者跟换注册账号
        if (employee!=null){
            throw new BusinessException("用户名已被注册，请直接登录");
        }
        //不存在，走新增,返回id
        employeeMapper.insert(admin);
        shop.setAdmin(admin);
        //新增之后返回id，在设置给员工表
        shopMapper.insert(shop);
        admin.setShopId(shop.getId());
        employeeMapper.update(admin);

    }
}
